const express = require('express')
const { ordersModel } = require('../model/ordersModel')
const router = express.Router()
// 查询所有的保险订单信息
router.get('/getAllorders', async (req, res) => {
    const result = await ordersModel.find().populate('insurancesId')
    if (result.length > 0) {
        res.send({
            code: 1,
            rows: result
        })
    }
})
// 按照指定的用户名进行查找
router.get('/searchOrders', async (req, res) => {
    const result = await ordersModel.find({ ordersName: { $regex: req.query.ordersName } }).populate('insurancesId')
    if (result.length > 0) {
        res.send({
            code: 1,
            message: '查询成功',
            rows: result
        })
    } else {
        res.send({
            code: 1,
            message: '查询失败'
        })
    }
})
// 删除用户信息
router.delete('/deleteOrders/:_id', async (req, res) => {
    const result = await ordersModel.deleteOne({ _id: req.params._id })
    try {
        if (result) {
            res.send({
                code: 1,
                message: '删除成功',
                rows: result
            })
        }
    } catch (err) {
        res.send({
            code: 0,
            message: '删除失败'
        })
    }
})
module.exports = router